package com.sw.test01.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sw.test01.R;
import com.sw.test01.domain.bean.News;
import com.sw.test01.ui.activity.NewsContentActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by xifanoo on 2017/3/23.
 * 这个是由主activity加载的，根据屏幕大小进行判定是单页显示还是双页显示
 */

public class NewsTitleFragment extends Fragment {
    private boolean isTwoPane;
    private RecyclerView newsTitleRecyclerview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_title,container,false);
        newsTitleRecyclerview = (RecyclerView) view.findViewById(R.id.news_title_recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        newsTitleRecyclerview.setLayoutManager(layoutManager);
        NewsAdapter adapter = new NewsAdapter(getNews());
        newsTitleRecyclerview.setAdapter(adapter);
        return view;
    }

    /**
     * 请求网络得到数据
     * @return
     */
    private List<News> getNews() {
        List<News> mList = new ArrayList<>();
        for (int i=0; i<100; i++){
            News news = new News();
            news.setTitle("This is new title" + i);
            news.setContent(getRandomLengthContent("this is new content" + i));
            mList.add(news);
        }
        return mList;
    }

    /**
     * 随机生成新闻内容
     * @param content
     * @return
     */
    private String getRandomLengthContent(String content){
        Random random = new Random();
        int lenth = random.nextInt(20) + 1;
        StringBuilder builder = new StringBuilder();
        for (int i=0; i<lenth; i++){
            builder.append(content);
        }
        return builder.toString();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getActivity().findViewById(R.id.news_content_layout) != null){
            isTwoPane = true;//可以找到news_content_layout布局时,为双页模式
        }else {
            isTwoPane = false;//找不到news_content_layout布局时，为单页模式
        }
    }
    public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
        private List<News> mList = new ArrayList<>();

        public NewsAdapter(List<News> mList) {
            this.mList = mList;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false);
            final ViewHolder holder = new ViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    News news = mList.get(holder.getAdapterPosition());
                    if (isTwoPane) {
                        //如果是双页，则刷新NewsContentFragment中的内容
                        NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
                        newsContentFragment.refresh(news.getTitle(), news.getContent());
                    } else {
                        //如果是单页，则直接开启NewsContentActivity
                        NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent());
                    }
                }
            });
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            News news = mList.get(position);
            holder.tvNewsTitle.setText(news.getTitle());
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvNewsTitle;

            public ViewHolder(View view) {
                super(view);
                tvNewsTitle = (TextView) view.findViewById(R.id.news_title);
            }
        }
    }
}
